/*
 * Copyright (C) 2010 Grummfy This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version. This program
 * is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details. You should have received a copy of the GNU General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>
 */

package org.android.bbox;

import org.android.bbox.activities.*;
import org.android.bbox.data.NoDatabaseException;
import org.android.bbox.data.Profil;
import org.android.bbox.data.ProfilNotFoundException;
import org.android.bbox.generic.AbstractListActivity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class bbox extends AbstractListActivity implements OnItemClickListener
{
	protected Profil profil;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main);

		// fill the list and add the listener for elements
		// String[] mainList = getResources().getStringArray(R.array.main_list_array);
		// Arrays.sort(mainList);
		/*
		 * //android.R.layout.simple_list_item_1 & 2 = définit par défaut //R.layout..... => xml de
		 * définition ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
		 * android.R.layout.simple_list_item_1, mainList); this.setListAdapter(adapter);
		 */
		Menu[] m = Menu.factory(getResources().getStringArray(R.array.main_list_array));
		ListView myList = (ListView) findViewById(android.R.id.list);
		ArrayAdapter<Menu> adapter = new ArrayAdapter<Menu>(this, android.R.layout.simple_list_item_1, m);
		adapter.sort(Menu.getComparator());
		myList.setAdapter(adapter);
		myList.setOnItemClickListener(this);

		// TODO use user preference and database

		// take the current profile, if not exist, ...
		try
		{
			this.profil = new Profil(this, 1);
		}
		catch (ProfilNotFoundException e)
		{
			//XXX get new profil or select one
			startActivity(new Intent(this, ProfilChoice.class));
		}
		catch (NoDatabaseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	{
		/*
		 * ArrayAdapter<String> adapter = (ArrayAdapter<String>) parent.getAdapter(); String
		 * bonnevaleur = adapter.getItem(position); Toast msg = Toast.makeText(this,
		 * "Vous avez cliqué sur : " + mainList[(int) id] + "(" + id + ":" + position + ") soit " +
		 * bonnevaleur, Toast.LENGTH_SHORT); msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
		 * msg.getYOffset() / 2); msg.show();
		 */

		Class myActivity = null;
		
		switch (((Menu)parent.getItemAtPosition(position)).getId())
		{
			default:
				Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("Error");
				builder.setMessage("This functionnality is not ready... try again on the next update!");
				builder.setPositiveButton("OK", null);
				builder.show();
				return;
			case 0:
				myActivity = Wifi.class;
				break;
			case 6:
				Box b = new Box(this.profil);
				String url = b.buildUrl("index.cgi?user_name=" + this.profil.getLogin() + "&password="
						+ this.profil.getPassword());
				Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
				startActivity(intent);
				break;
			case 1:
				myActivity = Log.class;
				break;
			//2 : config
			//3 : change password
			//4 : status
			//5 : other
		}

		if (myActivity != null)
		{
			startActivity(new Intent(this, myActivity));
		}
	}
}
